package com.foot.travel.adapter;

import java.util.ArrayList;
import java.util.List;

import org.hybridsquad.android.library.CropParams;

import com.foot.travel.R;
import com.koalac.ironman.common.module.BaseActivity;
import com.koalac.ironman.widget.ActionSheet;
import com.koalac.ironman.widget.ActionSheet.ActionSheetListener;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.LifeqImageView;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.GalleryFinal.OnHanlderResultCallback;
import cn.finalteam.galleryfinal.model.PhotoInfo;

public class UpLoadImageAdapter extends BaseAdapter implements OnHanlderResultCallback {

	int mMaxPhotoNum = 0;
	private ArrayList<String> mPhotoFilePaths = new ArrayList<String>();
	private ArrayList<String> mPhotoUrls = new ArrayList<String>();
	private ArrayList<Integer> fileIds = new ArrayList<Integer>();

	private int urlSize = 0;

	LayoutInflater mLayoutInflater;
	public String mClipStrategy;
	private Context mContext;

	public static final int SELECT_CAMERA = 0;
	public static final int SELECT_PHOTO = 1;
	public static final int REQUEST_CODE = 1;

	CropParams mCropParams = new CropParams();

	public UpLoadImageAdapter(Context context, int maxPhotoNum) {
		mMaxPhotoNum = maxPhotoNum;
		mLayoutInflater = LayoutInflater.from(context);
		mContext = context;

	}

	public void setPhotoUrlSize(int initUrlsize) {
		urlSize = initUrlsize;
	}

	public int getPhotoUrlSize() {
		return urlSize;
	}

	public void setClipStrategy(String clipStrategy) {
		mClipStrategy = clipStrategy;
	}

	public void clearUrl() {
		mPhotoUrls.clear();
	}

	private void removeFilePathItem(int position) {
		String deleteFilePath = mPhotoFilePaths.remove(position);
		/**
		 * 把放弃的图片文件从本地删除
		 */
		// File file = new File(deleteFilePath);
		// file.delete();
		notifyDataSetChanged();
	}

	private void removeUrlItem(int position) {
		mPhotoUrls.remove(position);
		if (position < fileIds.size()) {
			fileIds.remove(position);
		}
		urlSize--;
		notifyDataSetChanged();
	}

	public void addPhotoUrl(String url) {
		mPhotoUrls.add(url);
	}

	public void addFileId(int fileId) {
		fileIds.add(fileId);
	}

	public void addImage(String filePath) {
		mPhotoFilePaths.add(filePath);
		notifyDataSetChanged();
	}

	public ArrayList<String> getPhotoFilePaths() {
		return mPhotoFilePaths;
	}

	public void setmPhotoFilePaths(ArrayList<String> photoFilePaths) {
		this.mPhotoFilePaths = photoFilePaths;
	}

	@Override
	public int getCount() {
		int actualSize = getActualSize();
		if (actualSize < mMaxPhotoNum) {
			return actualSize + 1;
		}
		return actualSize;
	}

	public int getActualSize() {
		return mPhotoFilePaths.size() + urlSize;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		LifeqImageView lifeqImageView;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		// if (convertView == null) {
		convertView = mLayoutInflater.inflate(R.layout.upload_photo_item, null);
		viewHolder = new ViewHolder();
		viewHolder.lifeqImageView = (LifeqImageView) convertView.findViewById(R.id.upload_image);
		convertView.setTag(viewHolder);
		// }
		viewHolder = (ViewHolder) convertView.getTag();
		int actualSize = mPhotoFilePaths.size() + urlSize;
		/**
		 * 判断那个上传图片的图标是否要显示
		 */
		if (actualSize < mMaxPhotoNum && position == actualSize) {
			viewHolder.lifeqImageView.setImageResource(R.drawable.increase);
			viewHolder.lifeqImageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					mContext.setTheme(R.style.ActionSheetStyleIOS7);
					ActionSheet.createBuilder(mContext, ((BaseActivity) mContext).getSupportFragmentManager())
							.setOtherButtonTitles(new String[] { "拍照", "图库" }).setListener(new ActionSheetListener() {

						@Override
						public void onOtherButtonClick(ActionSheet actionSheet, int index) {
							FunctionConfig config;
							switch (index) {
							case SELECT_CAMERA:
								config = new FunctionConfig.Builder()
										.setMutiSelectMaxSize(mMaxPhotoNum - mPhotoFilePaths.size()).build();
								GalleryFinal.openCamera(REQUEST_CODE, config, UpLoadImageAdapter.this);
								break;
							case SELECT_PHOTO:
								config = new FunctionConfig.Builder()
										.setMutiSelectMaxSize(mMaxPhotoNum - mPhotoFilePaths.size()).build();
								GalleryFinal.openGalleryMuti(REQUEST_CODE, config, UpLoadImageAdapter.this);
								break;
							}
						}

						@Override
						public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

						}
					}).setCancelButtonTitle("取消").show();
				}
			});
			viewHolder.lifeqImageView.setOnLongClickListener(null);
		} else {
			if (position < urlSize) {
				viewHolder.lifeqImageView.loadFile(mPhotoUrls.get(position), ImageType.SourceImage, null, null);
				viewHolder.lifeqImageView.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						removeUrlItem(position);
						return true;
					}
				});
			} else {
				final int tempPosition = position - urlSize;
				Log.i("onHanlderSuccess", "tempPosition " + mPhotoFilePaths.get(tempPosition));
				viewHolder.lifeqImageView.displayLocalFile(mPhotoFilePaths.get(tempPosition), ImageType.SourceImage,
						null);
				viewHolder.lifeqImageView.setOnLongClickListener(new OnLongClickListener() {

					@Override
					public boolean onLongClick(View v) {
						removeFilePathItem(tempPosition);
						return true;
					}
				});
			}
			viewHolder.lifeqImageView.setOnClickListener(null);
		}
		return convertView;
	}

	public ArrayList<String> getPhotoUrls() {
		return mPhotoUrls;
	}

	public void setPhotoUrls(ArrayList<String> photoUrls) {
		this.mPhotoUrls = photoUrls;
		notifyDataSetChanged();
	}

	public ArrayList<Integer> getFileIds() {
		return fileIds;
	}

	public void setFileIds(ArrayList<Integer> fileIds) {
		this.fileIds = fileIds;
	}

	@Override
	public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
		for (PhotoInfo photoInfo : resultList) {
			addImage(photoInfo.getPhotoPath());
			Log.i("onHanlderSuccess", photoInfo.getPhotoPath());
		}
	}

	@Override
	public void onHanlderFailure(int requestCode, String errorMsg) {

	}

}
